package com.wecheir.service.impl;
import com.wecheir.dao.FarmDao;
import com.wecheir.entity.Farm;
import com.wecheir.entity.FarmData;
import com.wecheir.service.FarmService;
import com.wecheir.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 农田管理service实现层
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class FarmServiceImpl implements FarmService {
    @Resource
    UserService userService;

    @Resource
    FarmDao farmDao;
    /**
     * 获得同一农田名字的数目
     * @param name
     * @return
     */
    @Override
    public long getFarmNameCount(String name) {
//        QueryWrapper<Farm> wrapper = new QueryWrapper<>();
//        wrapper.eq("name",name);
        return farmDao.getFarmNameCount(name);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Farm saveFarm(Farm farm) {
        String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
      farmDao.saveFarmMenus(farm,uuid);
        return farm;
    }

    @Override
    public Farm getFarmById(int id) {
        return farmDao.selectFarmById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateFarm(Farm farm) { farmDao.updateById(farm);
    }

    @Override
    public void deleteFarm(int id) {
//        farm.setDelFlag(true);
        farmDao.updateFarm(id);
    }

//    @Override
//    public void addFarm(Farm farm) {
//
//        farmDao.addFarm(farm);
//    }

    @Override
    public List<Farm> selectAll() {
        return new ArrayList<>();
    }

    @Override
    public List<Farm> getFarmByUserId(String userId) {
        return null;
//        return farmDao.getFarmByUserId(userId);
    }

    @Override
    public List<Farm> getFarmByUser(String id, int start, int limit) {
        return farmDao.getFarmByUser(id,start,limit);


    }


    @Override
    public List<FarmData> getFarmData(String farmId, Date beginDate, Date endDate) {

        return farmDao.getFarmData(farmId,beginDate,endDate);

    }

    @Override
    public boolean updateImg(int id, String url) {

        return farmDao.updateFarmImg(id, url) == 1;

    }
}
